/**
 *
 */
package service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import common.consts.Consts;
import dao.AccountDao;
import dao.WorkPreferenceDao;
import domain.Account;
import domain.WorkPreference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.ServiceResponse;
import service.WorkPreferenceService;
import wrapper.user.Preference;

import java.io.IOException;
import java.util.Date;

/**
 * Created by chen <chen@youjianghr.com> on 2015/7/13.
 *
 * @author chen <chen@youjianghr.com>
 */

@Service
public class WorkPreferenceServiceImpl extends ServiceBase implements WorkPreferenceService {

    private static final Logger logger = LoggerFactory.getLogger(WorkPreferenceServiceImpl.class);

    private static final ObjectMapper JSON_PRASER = new ObjectMapper();

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private WorkPreferenceDao workPreferenceDao;

    /* 保存或更新用户设置的偏好
     * @see service.WorkPreferenceService#saveOrUpdateUserPreference(long, wrapper.user.Preference)
     *
     * @param id 用户id
     * @param workPreference 封装用户设置的偏好
     */
    @Override
    @Transactional(readOnly = false)
    public ServiceResponse saveOrUpdateUserPreference(final long id, final Preference workPreference) {

        Account account = accountDao.get(id);
        if (account == null) {
            return ServiceResponse.fail(Consts.i18nKey.INVALID_USER_ID);
        }

        WorkPreference preferenceDB = workPreferenceDao.get(id);

        String preferenceJson = null;
        try {
            preferenceJson = JSON_PRASER.writeValueAsString(workPreference);
        } catch (JsonProcessingException e) {
            logger.error("error when prase to json.", e);
            return ServiceResponse.fail(Consts.i18nKey.SYSTEM_ERROR);
        }

        if (preferenceDB == null) {
            preferenceDB = new WorkPreference(id);
            preferenceDB.setPreference(preferenceJson);
            preferenceDB.setUpdateTime(new Date());

            workPreferenceDao.insert(preferenceDB);

        } else {
            preferenceDB.setPreference(preferenceJson);
            preferenceDB.setUpdateTime(new Date());
            workPreferenceDao.update(preferenceDB);
        }

        return ServiceResponse.success();
    }

    /* 获得用户设置的偏好，加载的响应视图中
     * @see service.WorkPreferenceService#getUserPreference(long)
     *
     * @param id 用户id
     */
    @Override
    @Transactional(readOnly = true)
    public ServiceResponse getUserPreference(long id) {
        Account account = accountDao.get(id);
        if (account == null) {
            return ServiceResponse.fail(Consts.i18nKey.INVALID_USER_ID);
        }

        WorkPreference workPreference = workPreferenceDao.get(id);
        if (workPreference == null) {
            return ServiceResponse.fail(Consts.i18nKey.WORK_PREFERENCE_EMPTY);
        }

        Preference preference = null;
        try {
            preference = JSON_PRASER.readValue(workPreference.getPreference(), Preference.class);
        } catch (IOException e) {
            logger.error("error when prasing json", e.getMessage());
            return ServiceResponse.fail(Consts.i18nKey.SYSTEM_ERROR);
        }
        return ServiceResponse.success().with("workPreference", preference);
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void setWorkPreferenceDao(WorkPreferenceDao workPreferenceDao) {
        this.workPreferenceDao = workPreferenceDao;
    }

}
